<?php defined('SYSPATH') or die('No direct script access.');

class FileUtil{

	public static function Factory(){
		return new FileUtil();
	}

	public static function setFilePath($fileName,$colName){
		if ($fileName!=FALSE){
			$list = explode(DIRECTORY_SEPARATOR,$fileName);
			$imgurl=$list[sizeof($list)-1];
			$_POST[$colName] = $imgurl;
		}
	}

	public static function getFilePath($fileName){
		if ($fileName!=FALSE){
			$list = explode(DIRECTORY_SEPARATOR,$fileName);
			$imgurl=$list[sizeof($list)-1];
			return $imgurl;
		}
		return NULL;
	}

	public static function saveFile($directory){
		$array = Validate::factory($_FILES);
		$array->rule('file', 'Upload::valid');
		$array->rule('file', 'Upload::not_empty');

		if ($array->check())
		{
			return Upload::save($_FILES['file'],null,$directory);
		}
		return FALSE;
	}
	
	public static function saveFilesForCoupon($directory){
		$result = FALSE;
		if (array_key_exists("smallpicture", $_FILES))
		{
			$result["smallpicture"] = Upload::save($_FILES['smallpicture'],null,$directory);
		}
		if (array_key_exists("largepicture", $_FILES))
		{
			$result["largepicture"] = Upload::save($_FILES['largepicture'],null,$directory);
		}
		return $result;
	}

	public static function deleteFile($filename){
		$filename = realpath($filename);
		if(file_exists($filename)){
			unlink($filename);
		}
	}
}
